package com.cheer.ba.view.activity;

import android.graphics.Color;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.andview.refreshview.XRefreshView;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.listener.OnItemClickListener;
import com.cheer.ba.model.BankModel;
import com.cheer.ba.presenter.impl.AskQuestPresenter;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.view.adapter.FrequentlyAreaAdp;
import com.cheer.ba.view.adapter.MyBankAdp;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.widget.headview.CheerHeadView;
import com.cheer.lib_recycle_swipe.Closeable;
import com.cheer.lib_recycle_swipe.OnSwipeMenuItemClickListener;
import com.cheer.lib_recycle_swipe.SwipeMenu;
import com.cheer.lib_recycle_swipe.SwipeMenuCreator;
import com.cheer.lib_recycle_swipe.SwipeMenuItem;
import com.cheer.lib_recycle_swipe.SwipeMenuRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Hq on 2017/7/31.
 * 我的银行卡列表
 */
public class MyBankListAty extends BaseCommAty {

    @BindView(R.id.id_bank_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_bank_recyclev)
    SwipeMenuRecyclerView mRecycleView;
    private MyBankAdp mMyBankAdp;
    private List<BankModel> mAllBanks = new ArrayList<BankModel>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_my_bank_list, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initPresenter() {
        presenter = new AskQuestPresenter();
    }

    @Override
    public void initView() {
        setTitleText("我的银行卡");
        mAllBanks.clear();
        mAllBanks.add(handList());

        setXRefreshView(mXRefreshView, true, false, true, false, new CheerHeadView(aty), null);
        mRecycleView.setLayoutManager(new LinearLayoutManager(aty));
        mRecycleView.setHasFixedSize(true);
        mRecycleView.setItemAnimator(new DefaultItemAnimator());
        // 为SwipeRecyclerView的Item创建菜单就两句话，不错就是这么简单：
        // 设置菜单创建器。
        mRecycleView.setSwipeMenuCreator(swipeMenuCreator);
        // 设置菜单Item点击监听。
        mRecycleView.setSwipeMenuItemClickListener(menuItemClickListener);
        mRecycleView.smoothOpenRightMenu(0);
        mRecycleView.setNestedScrollingEnabled(false);

        mMyBankAdp = new MyBankAdp(mAllBanks);
        mMyBankAdp.setOnItemClickListener(onItemClickListener);
        mRecycleView.setAdapter(mMyBankAdp);
    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false);
            }
        });
    }

    /**
     * 加载数据
     */
    public void loadData(boolean isShowLoad) {
        ((AskQuestPresenter) presenter).getBankList(isShowLoad);
        stopRefresh(mXRefreshView, 2000);
    }


    /**
     * 菜单创建器。在Item要创建菜单的时候调用。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int size = getResources().getDimensionPixelSize(R.dimen.item_height1);
            if (viewType == FrequentlyAreaAdp.VIEW_TYPE_NONE) {
                //不添加滑动
            } else {
                SwipeMenuItem addItem = new SwipeMenuItem(aty)
                        .setBackgroundDrawable(getResources().getDrawable(R.color.color_ff3b32))
//                .setImage(R.drawable.ic_home_msg)
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setTextSize(16)
                        .setHeight(size)
                        .setWidth(size);
                swipeRightMenu.addMenuItem(addItem);
            }
        }
    };

    private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() {
        /**
         * Item的菜单被点击的时候调用。
         * @param closeable       closeable. 用来关闭菜单。
         * @param adapterPosition adapterPosition. 这个菜单所在的item在Adapter中position。
         * @param menuPosition    menuPosition. 这个菜单的position。比如你为某个Item创建了2个MenuItem，那么这个position可能是是 0、1，
         * @param direction       如果是左侧菜单，值是：SwipeMenuRecyclerView#LEFT_DIRECTION，如果是右侧菜单，值是：SwipeMenuRecyclerView#RIGHT_DIRECTION.
         */
        @Override
        public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) {
            // 关闭被点击的菜单。
            closeable.smoothCloseMenu();
            if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                BankModel model = mMyBankAdp.getItem(adapterPosition);
                //右侧菜单
                if (menuPosition == 0) {
                    //删除银行卡
                    ((AskQuestPresenter) presenter).delMyBank(true, model.bankNo);
                }
            }
        }
    };

    private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            BankModel model = mMyBankAdp.getItem(position);
            LogUtils.LogD("-----", model.flag + "========");
            if (null != model) {
                if (model.flag) {
                    //添加银行卡
                    showActivity(aty, AddMyBankAty.class);
                } else {
                    //列表点击
                    BaseEvent event = new BaseEvent();
                    event.type = ConstantTag.GET_MY_BANKS;
                    event.model = model;
                    EventBus.getDefault().post(event);
                    finish();
                }
            }
        }
    };

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        mXRefreshView.stopRefresh();
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_MY_BANKS)) {
                //获得我的银行卡列表
                final List<BankModel> banks = (List<BankModel>) t;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAllBanks.clear();
                        if (banks.isEmpty()) {
                            //没有银行卡
                        } else {
                            //有银行卡
                            mAllBanks.addAll(banks);
                        }
                        mAllBanks.add(handList());
                        mMyBankAdp.notifyDataSetChanged();
                    }
                });
            } else if (TextUtils.equals(tag, ConstantTag.DEL_MY_BANKS)) {
                //删除银行卡
                mXRefreshView.startRefresh();
            }
        }
    }

    public BankModel handList() {
        BankModel model = new BankModel();
        model.flag = true;
        return model;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        //返回类型
        String type = event.type;
        if (TextUtils.equals(type, ConstantTag.ADD_MY_BANKS)) {
            mXRefreshView.startRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
